<?php
namespace Web\Logic;
class RoleLogic {


	/**
	 *
	 * @param 栏目 $menuList
	 * @param 角色栏目 $roleMenu
	 */
	function roleMenuDiv($menuList,$roleMenu){
		//分割数据
		$roleM = explode(",", $roleMenu);
		//返回值
		$rdata = "";
		//判断
		if($menuList){
			foreach ($menuList as $m){
				$judm = "";
				if(in_array($m['id'],$roleM)) $judm = "checked='checked'";
				$rdata .="<tr><th><label class='position-relative'><input  class='ace' type='checkbox' $judm name='rolemenu[]' tly='menu' pvl='' vl='$m[id]' value='$m[id]' /><span class='lbl'></span></label>$m[text]</th></tr>";
				$rdata .= $this->DealWithMenus($m['items'], $roleM,1,$m[id]);
			}
		}
		return $rdata;
	}

	/**
	 * 数据处理
	 * @param 子栏目 $data
	 * @param 角色栏目 $roleM
	 * @param 父id
	 */
	function DealWithMenus($data,$roleM,$num,$pid){
		//空格
		$kgstr = "";
		$num = $num ? $num : 1;
		for($i=0; $i<$num;$i++){
			$kgstr .="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}
		$num +=1;
		//返回值
		$rsdata = "";
		//判断是否存在值
		if($data){
			//循环数据
			foreach ($data as $d){
                //如果有子栏目
                if(in_array($d['id'],$roleM)){
                 $judm = "checked='checked2'";
                 }else{
                    $judm="";
                 }
                 $rsdata .= "<tr><td>";
                 $rsdata .= $kgstr."<lable class='position-relative'><input type='checkbox' class='ace' $judm name='rolemenu[]' tly='menu' vl='$d[id]' pvl='$pid' value='$d[id]' /><span class='lbl'></span></lable>$d[text]";
                 $rsdata .= "</td></tr>".$kgstr;
                if($d['items']){
                     $rsdata .= $this->DealWithMenus($d['items'],$roleM,$num,$d['id']);
                }

			}
		}
		return $rsdata;
	}

	/**
	 * 保存角色栏目
	 * @param unknown $data
	 */
	function saveRoleMenu($data,$id){
		try {
			//返回值
			$rdata = false;
			//实例化模块
			$mod = M("role_info");
			//查询 角色是否在
			$n = $mod->where("roleid = $id")->field("roleid")->count();
			if($n>0){
				$rdata = $mod->where("roleid = $id")->save($data);
			}else{
				$data['roleid']=$id;
				$rdata = $mod->data($data)->add();
			}
			return $rdata;
		} catch (Exception $e) {
		}
	}
	/******************************sql****************************************************/
	/**
	 * 添加数据
	 * @param unknown $data
	 * @return Ambigous <mixed, boolean, unknown, string>
	 */
	function addDataSql($data){
		try {
			//实例化模块
			$mod = M("role");
		    return $mod->data($data)->add();
		} catch (Exception $e) {
		}
	}

	/**
	 * 查询个数
	 * @param unknown $data
	 */
	function selNum($wdata){
		try {
			//实例化模块
			$mod = M("role");
			//查询条件
			return  $mod->where($wdata)->field('roleid')->count();
		} catch (Exception $e) {
		}
	}

	/**
	 * 查询数据
	 * @param unknown $wdata
	 * @param unknown $Page
	 */
	function indexList($wdata,$Page){
		try {
			//实例化模块
			$mod = M("role");
			$rdata = $mod->field("roleid,rolename,isforbidden,updatedatetime")
	                     ->where($wdata)
	                     ->order('roleid DESC')
	                     ->limit($Page->firstRow.','.$Page->listRows)
	                     ->select();

			return $rdata;
		} catch (Exception $e) {
		}
	}

	/**
	 * 删除数据
	 * @param unknown $roleid
	 */
	function delDataSql($roleid){
		try {
			//实例化模块
			$mod = M("role");
			return $mod->where("roleid = $roleid")->delete();
		} catch (Exception $e) {
		}
	}

	/**
	 * 修改数据
	 * @param unknown $data
	 * @param unknown $roleid
	 */
    function editDataSql($data,$roleid){
    	try {
    		//实例化模块
    		$mod = M("role");
    		return $mod->where("roleid = '$roleid'")->save($data);
    	} catch (Exception $e) {
    	}
    }

    /**
     * 修改数据
     * @param unknown $data
     * @param unknown $roleid
     */
    function selOneSql($roleid){
    	try {
    		//实例化模块
    		$mod = M("role");
    		return $mod->where("roleid = '$roleid'")
                    ->field('roleid,rolename,isforbidden,notes')
                    ->find();;
    	} catch (Exception $e) {
    	}
    }

    /**
     * 查询角色栏目权限
     * @param unknown $id
     */
    function selRoleMenu($id){
    	try {
    		//实例化模块
    		$mod = M("role_info");
    		$rdata = $mod->field("roleid,menuroot")->where("roleid = $id")->find();
    		return $rdata['menuroot'];
    	} catch (Exception $e) {
    		return "";
    	}
    }
}